package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.MainActivity;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.Params;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: ChargingSetActivity
 * @Description: 收费设置页面
 * @Author: ljp
 * @CreateDate: 2018/11/7 11:40
 * @Version: 1.0
 */
public class ChargingSetActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_value)
    TextView tvValue;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.et_price)
    EditText etPrice;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String userId;
    private String token;
    private String deviceInfo;
    private String TAG = "ChargingSetActivity";

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_charging_set;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        userId = mySeleInfo.getId();
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        initHttpData();
    }
    /**
     * @return
     * @method
     * @description 数据请求
     * @date: 2018/11/16 17:16
     * @author: ljp
     */
    private void initHttpData() {
        MeInfoService.goldDiamond(userId, token, deviceInfo).subscribe(goldBean -> {
            //Log.i(TAG, "initHttp: -------"+goldBean.getObject().toString());
            if (goldBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                tvValue.setText(goldBean.getObject().getTotalIncome() + "");
                if(goldBean.getObject().getTotalIncome() < 30000){
                    etPrice.setText(30+"");
                    etPrice.setEnabled(false);
                    tvSave.setEnabled(false);
                }else if(goldBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                    Utils.setToken(context);
                }else {
                    toast_MakeText(goldBean.getMsg());
                }
            } else {
                toast_MakeText(goldBean.getMsg());
            }
        }, throwable -> {
            Log.i(TAG, "initHttp: -----" + throwable.toString());
        });
    }
    /**
    * @method
    * @description 保存价格
    * @date: 2018/11/27 11:39
    * @author: ljp
    * @return
    */
    private void initSave() {
        int endPrice = 30;//最低价格
        int price = Integer.parseInt(etPrice.getText().toString().trim());//输入的价格
        int trim = Integer.parseInt(tvValue.getText().toString().trim());//魅力值
       //根据魅力值，判断输入的价格
        if (trim >= 30000 && trim < 50000){
           endPrice = price <= 30 ? 30:price >= 50 ? 50:price;
       }else if (trim >= 50000 && trim < 80000){
           endPrice = price <= 30 ? 30:price >= 60 ? 60:price;
       }else if (trim >= 80000 && trim < 100000){
           endPrice = price <= 30 ? 30:price >= 80 ? 80:price;
       }else if (trim >= 100000){
            endPrice = price <= 30 ? 30:price >= 100 ? 100:price;
        }
       Params params = new Params();
       params.put("id", mySeleInfo.getId());
       params.put("mobile", SPUtils.getEditoString(SPUtils.SP_PHONE));//手机号码
        params.put("token", token);//token
        params.put("equipmentId",deviceInfo);//设备号
        params.put("gold",String.valueOf(endPrice));//设备号
        int finalEndPrice = endPrice;
        MeInfoService.updateData(params).subscribe(mySelfInfoBean -> {
           // Log.i(TAG, "initSave: ----"+mySelfInfoBean.getObject().toString());
            if(mySelfInfoBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS){
                mySeleInfo.setGold(mySelfInfoBean.getObject().getGold());
                SPUtils.setEditorKeyValue(SPUtils.SP_MYSELFINFO, ParcelableUtils.marshall(mySeleInfo));
                Intent intent = new Intent(activity, MainActivity.class);
                intent.putExtra("flag",4);
                startActivity(intent);
                finish();
            }
        },throwable -> {
            toast_MakeText(getResources().getString(R.string.net_error));
            Log.i(TAG, "initSave: ----"+throwable);
        });
    }

    @OnClick({R.id.linear_back, R.id.tv_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.tv_save:
                initSave();
                break;
        }
    }
}
